#include "MyWindow.h"
#include "ElaTheme.h"
#include "ElaApplication.h"
#include <QPushButton>
#include <QHBoxLayout>

MyWindow::MyWindow(QWidget* parent) : ElaWidget(parent)
{
    // 设置窗口属性
    setWindowTitle("我的应用程序"); // 设置窗口标题
    resize(800, 600);

    // 居中显示
    moveToCenter();

    // 连接信号
    connect(this, &MyWindow::closeButtonClicked, this, &MyWindow::onClose);
    //connect(this, &MyWindow::themeChangeButtonClicked, this, &MyWindow::onThemeChange);
    connect(this, &MyWindow::navigationButtonClicked, this, &MyWindow::onNavigationClicked);
    // // 设置窗口按钮（禁用所有按钮）
    //setWindowButtonFlags(ElaAppBarType::NoneButtonHint);
    // 设置窗口按钮（启用所有按钮: 置顶, 最小化, 最大化, 关闭, 主题切换, 导航）
    setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

    // 添加内容
    setupUI();
}

void MyWindow::setupUI()
{
    // 创建界面内容
    m_titleLabel = new QLabel("欢迎使用 ElaWidget", this);
    m_titleLabel->setAlignment(Qt::AlignCenter);
    m_titleLabel->setStyleSheet("font-size: 24px; font-weight: bold; margin: 20px;");

    m_contentLabel = new QLabel("这是一个使用 ElaWidget 创建的现代化窗口示例", this);
    m_contentLabel->setAlignment(Qt::AlignCenter);
    m_contentLabel->setStyleSheet("font-size: 14px; color: gray; margin: 10px;");

    // 创建操作按钮
    QPushButton* testButton = new QPushButton("测试按钮", this);
    testButton->setFixedSize(120, 40);
    connect(testButton, &QPushButton::clicked, this, []() { QMessageBox::information(nullptr, "提示", "按钮点击成功！"); });

    QPushButton* centerButton = new QPushButton("重新居中", this);
    centerButton->setFixedSize(120, 40);
    connect(centerButton, &QPushButton::clicked, this, &MyWindow::moveToCenter);

    // 按钮布局
    QHBoxLayout* buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch();
    buttonLayout->addWidget(testButton);
    buttonLayout->addWidget(centerButton);
    buttonLayout->addStretch();

    // 主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, getAppBarHeight() + 20, 20, 20); // 设置内容边距
    mainLayout->setSpacing(20);
    mainLayout->addStretch();
    mainLayout->addWidget(m_titleLabel);
    mainLayout->addWidget(m_contentLabel);
    mainLayout->addLayout(buttonLayout);
    mainLayout->addStretch();

    setLayout(mainLayout);
}

void MyWindow::onClose()
{
    // 自定义关闭逻辑
    if (confirmClose())
    {
        close();
    }
}

bool MyWindow::confirmClose()
{
    QMessageBox::StandardButton result = QMessageBox::question(this, "确认关闭", "确定要关闭窗口吗？", QMessageBox::Yes | QMessageBox::No, QMessageBox::No);

    return result == QMessageBox::Yes;
}

void MyWindow::onThemeChange()
{
    // 切换主题
    ElaThemeType::ThemeMode currentMode = eTheme->getThemeMode();
    ElaThemeType::ThemeMode newMode = (currentMode == ElaThemeType::Light) ? ElaThemeType::Dark : ElaThemeType::Light;

    eTheme->setThemeMode(newMode);

    // 更新标签文本
    QString themeName = (newMode == ElaThemeType::Light) ? "浅色" : "深色";
    m_contentLabel->setText(QString("当前主题：%1模式").arg(themeName));
}

void MyWindow::onNavigationClicked()
{
    QMessageBox::information(this, "导航", "导航按钮被点击！");
}